package com.hxc;

import com.hxc.configs.db.PrimeDB;
import com.hxc.user.UserData;
import java.sql.Connection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserWebServicer {

  @Autowired
  private PrimeDB primeDB;

  @GetMapping("/findUser")
  public UserData findUser(@RequestParam("userId") String userId) throws Exception {
    System.out.println("Received request with userId: " + userId);
    try (Connection conn = primeDB.create()) {
      return new UserService().findUser(conn, userId);
    } catch (Exception e) {
      throw new Exception(e);
    }
  }

  @PostMapping("/insertUser")
  public String sendMessage(@RequestBody UserData userData) throws Exception {
    try (Connection conn = primeDB.create()) {
      return new UserService().insertUser(conn, userData);
    } catch (Exception e) {
      throw new Exception(e);
    }
  }
}
